﻿using Spire.Doc;
using Spire.Doc.Documents;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace WordCompareCore.Utils
{
    public class WordHelper
    {
        #region 转为图片
        /// <summary>
        /// 将word文档的字节流转成base64格式的图片数据集合
        /// </summary>
        /// <param name="docBytes"></param>
        /// <returns></returns>
        public static List<string> Convert2Images(byte[] docBytes)
        {
            List<string> listImageData = new List<string>();

            using (Stream sm = new MemoryStream(docBytes))
            {
                Document doc = new Document(sm);
                if (doc == null)
                {
                    throw new Exception("Word无效或者文档已被加密无法读取。");
                }
                Image[] images = doc.SaveToImages(ImageType.Bitmap);
                for (int i = 0; i < images.Length; i++)
                {
                    //string path = $"d:/image-{i + 1}.png";
                    using (var pageSm = new MemoryStream())
                    {
                        images[i].Save(pageSm, ImageFormat.Png);
                        var pageBytes = new byte[pageSm.Length];
                        pageSm.Position = 0;
                        pageSm.Read(pageBytes, 0, pageBytes.Length);
                        listImageData.Add(Convert.ToBase64String(pageBytes));
                        //images[i].Save(path);
                    }
                }
            }

            return listImageData;
        }
        /// <summary>
        /// 将word文档的字节流转成base64格式的图片数据集合
        /// </summary>
        /// <param name="docStream"></param>
        /// <returns></returns>
        public static List<string> Convert2Images(Stream docStream)
        {
            List<string> listImageData = new List<string>();

            Document doc = new Document(docStream);
            if (doc == null)
            {
                throw new Exception("Word无效或者文档已被加密无法读取。");
            }
            Image[] images = doc.SaveToImages(ImageType.Bitmap);
            for (int i = 0; i < images.Length; i++)
            {
                //string path = $"d:/image-{i + 1}.png";
                using (Stream pageSm = new MemoryStream())
                {
                    images[i].Save(pageSm, ImageFormat.Png);
                    var pageBytes = new byte[pageSm.Length];
                    pageSm.Position = 0;
                    pageSm.Read(pageBytes, 0, pageBytes.Length);
                    listImageData.Add(Convert.ToBase64String(pageBytes));
                    //images[i].Save(path);
                }
            }

            return listImageData;
        }
        /// <summary>
        /// 将word文档的字节流转成base64格式的图片数据集合
        /// </summary>
        /// <param name="docFilePath">word文档绝对路径</param>
        /// <returns></returns>
        public static List<string> Convert2Images(string docFilePath)
        {
            List<string> listImageData = new List<string>();

            Document doc = new Document(docFilePath);
            if (doc == null)
            {
                throw new Exception("Word无效或者文档已被加密无法读取。");
            }
            Image[] images = doc.SaveToImages(ImageType.Bitmap);
            for (int i = 0; i < images.Length; i++)
            {
                //string path = $"d:/image-{i + 1}.png";
                using (Stream pageSm = new MemoryStream())
                {
                    images[i].Save(pageSm, ImageFormat.Png);
                    var pageBytes = new byte[pageSm.Length];
                    pageSm.Position = 0;
                    pageSm.Read(pageBytes, 0, pageBytes.Length);
                    listImageData.Add(Convert.ToBase64String(pageBytes));
                    //images[i].Save(path);
                }
            }

            return listImageData;
        }
        #endregion

        #region 转为PDF
        /// <summary>
        /// 将word文档字节流转为PDF文档流
        /// </summary>
        /// <param name="docBytes"></param>
        /// <returns></returns>
        public static Stream Convert2Pdf(byte[] docBytes)
        {
            using (Stream sm = new MemoryStream(docBytes))
            {
                Document doc = new Document(sm);
                if (doc == null)
                {
                    throw new Exception("Word无效或者文档已被加密无法读取。");
                }
                using (var pdfSm = new MemoryStream())
                {
                    doc.SaveToFile(pdfSm, FileFormat.PDF);
                    pdfSm.Position = 0;
                    return pdfSm;
                }
            }
        }
        #endregion
    }
}
